package com.mygame.components

import com.almasb.fxgl.dsl.image
import com.almasb.fxgl.entity.component.Component
import com.almasb.fxgl.entity.state.EntityState
import com.almasb.fxgl.entity.state.StateComponent
import com.almasb.fxgl.texture.AnimatedTexture
import com.almasb.fxgl.texture.AnimationChannel
import com.mygame.Const
import javafx.beans.value.ObservableValue
import javafx.util.Duration

val IDLE = EntityState.IDLE
val RUN = EntityState("RUN")
val ATTACK = EntityState("ATTACK")
val JUMP_ATTACK = EntityState("JUMP_ATTACK")
val SKILL1 = EntityState("SKILL1")

class PlayerStateComponent : Component() {

    private var r: Double = 0.0
    private lateinit var stateComponent: StateComponent

    private val stateChannelMap = mutableMapOf<EntityState, AnimationChannel>()
    override fun onAdded() {
        stateComponent = entity.getComponent(StateComponent::class.java)
        if (entity.properties.exists(Const.RADIUS)) {
            r = entity.properties.getDouble(Const.RADIUS)
        }
        initResAndBindState()
    }

    private fun initResAndBindState() {
        val scale = 1
        val width = 1536
        val height = 1280
        val framesPerRow = 6
        val fw = width / scale / framesPerRow //宽度只有6张
        val fh = height / scale / 10 //高度有10张图片
        val rawImg = image("sword_2x.png", width / scale.toDouble(), height / scale.toDouble())
        println("rawImg.raww:${rawImg.width}")
        val animIdle = AnimationChannel(rawImg, framesPerRow, fw, fh, Duration.seconds(0.8), 0, 5)
        stateChannelMap[IDLE] = animIdle
        val animWalk = AnimationChannel(rawImg, framesPerRow, fw, fh, Duration.seconds(0.8), 12, 17)
        stateChannelMap[RUN] = animWalk
        val animAttack = AnimationChannel(rawImg, framesPerRow, fw, fh, Duration.seconds(0.8), 6, 11)
        stateChannelMap[ATTACK] = animAttack
        val animJumpAttack = AnimationChannel(rawImg, framesPerRow, fw, fh, Duration.seconds(0.8), 6, 11)
        stateChannelMap[JUMP_ATTACK] = animJumpAttack
        val animSkill1 = AnimationChannel(rawImg, framesPerRow, fw, fh, Duration.seconds(0.8), 55, 57)
        stateChannelMap[SKILL1] = animSkill1
        val animatedTexture = AnimatedTexture(animIdle)
        animatedTexture.translateX = -fw * 0.4//左边的留白去掉
        animatedTexture.translateY = -fh * 0.6 - (fh * 0.4 - r)//上面的留白和人物的间距
        animatedTexture.loop()
        entity.viewComponent.addChild(animatedTexture)

        animatedTexture.onCycleFinished = Runnable {
            if (animatedTexture.animationChannel == animAttack
                || animatedTexture.animationChannel == animJumpAttack
                || animatedTexture.animationChannel == animSkill1
            ) {
                stateComponent.changeState(IDLE)
            }
        }

        stateComponent.currentStateProperty()
            .addListener { o: ObservableValue<out EntityState>?, oldState: EntityState?, newState: EntityState ->
                println("new state: $newState")
                stateChannelMap[newState]?.let {
                    animatedTexture.loopAnimationChannel(it)
                }
            }
    }
}